package com.FIlter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = {"/Home.jsp","/welcome.jsp","/includes/*","/PSales/salas.jsp","/system/uppass.jsp","/stock/*"})
public class PdminFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session =request.getSession();
        if(session != null && session.getAttribute("user") != null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            request.setAttribute("userLoginMess","请先登录");
            request.getRequestDispatcher("/index.jsp").forward(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
